package org.example.service;

import jakarta.annotation.Resource;
import org.example.dto.ProductRequestDto;
import org.example.dto.ProductResponseDto;
import org.example.bean.Product;
import org.example.dao.ProductDao;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductService {
  @Resource private  ProductDao productDao;
    public void create(ProductRequestDto request){
        Product product = Product.builder()
                .name(request.getName())
                .description(request.getDescription())
                .price(request.getPrice())
                .build();
        productDao.save(product);
    }
    public List<ProductResponseDto> findAll(){
        return productDao.findAll().stream()
                .map(product->ProductResponseDto.builder()
                        .id(product.getId())
                        .name(product.getName())
                        .description(product.getDescription())
                        .price(product.getPrice())
                        .build())
                .collect(Collectors.toList());
    }
}
